//
//  StoreValue.m
//  StoreValue
//
//  Created by JuYuFeng on 16/3/3.
//  Copyright © 2016年 hangting. All rights reserved.
//

#import "StoreValue.h"
#import "FastCoder.h"

@implementation StoreValue

+ (StoreValue *)sharedInstance {

    static StoreValue *storeValue = nil;
    
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        
        storeValue = [[StoreValue alloc] init];
    });
    
    return storeValue;
}
//-----------------------------------------------------------------------------------
/*
 这里是存储在[NSUserDefaults standardUserDefaults] 快捷方法, 传说是目前最快最好的本地化存储方式, 优先使用, 可以直接存储model,而不需要解码
 */
- (void)storeValue:(id)value withKey:(NSString *)key {

    NSParameterAssert(value);
    NSParameterAssert(key);
    
    NSData *data = [FastCoder dataWithRootObject:value];
    if (data) {
        
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
    }
}
- (id)valueWithKey:(NSString *)key {

    NSParameterAssert(key);
    
    NSData *data = [[NSUserDefaults standardUserDefaults] valueForKey:key];
    
    return [FastCoder objectWithData:data];
}


@end




















